---
id: polling
title: Polling
sidebar_label: Polling
hide_title: true
description: 'RTK Query > Usage > Polling: re-fetching data on a timer'
---

&nbsp;

# Polling

## Polling Overview

Polling gives you the ability to have a 'real-time' effect by causing a query to run at a specified interval. To enable polling for a query, pass a `pollingInterval` to the `useQuery` hook or action creator with an interval in milliseconds:

:::tip
Polling additionally has the ability to skip sending requests while the window is out of focus. To enable this behavior, pass `skipPollingIfUnfocused: true` to the `useQuery` hook or action creator.

_Note: `skipPollingIfUnfocused` requires [`setupListeners`](../api/setupListeners.mdx) to have been called._
:::

```tsx no-transpile title="src/Pokemon.tsx" no-transpile
import * as React from 'react'
import { useGetPokemonByNameQuery } from './services/pokemon'

export const Pokemon = ({ name }: { name: string }) => {
  // Automatically refetch every 3s unless the window is out of focus
  const { data, status, error, refetch } = useGetPokemonByNameQuery(name, {
    pollingInterval: 3000,
    skipPollingIfUnfocused: true,
  })

  return <div>{data}</div>
}
```

In an action creator without React Hooks:

```ts no-transpile
const { data, status, error, refetch } = store.dispatch(
  endpoints.getCountById.initiate(id, {
    subscriptionOptions: { pollingInterval: 3000 },
  }),
)
```

## Polling Without React Hooks

If you use polling without the convenience of React Hooks, you will need to manually call `updateSubscriptionOptions` on the promise ref to update the interval. This approach varies by framework but is possible everywhere. See the [Svelte Example](./examples#svelte) for one possibility, and the [Usage Without React Hooks](./usage-without-react-hooks.mdx) page for more details on working with subscriptions manually.

```ts no-transpile
queryRef.updateSubscriptionOptions({ pollingInterval: 0 })
```

## Polling Example

<iframe
  src="https://codesandbox.io/embed/github/reduxjs/redux-toolkit/tree/master/examples/query/react/polling?fontsize=12&runonclick=1&hidenavigation=1&theme=dark"
  style={{
    width: '100%',
    height: '600px',
    border: 0,
    borderRadius: '4px',
    overflow: 'hidden',
  }}
  title="rtk-query-react-hooks-example"
  allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
  sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
></iframe>
